package com.kinptm.masterclock.ui;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.kinptm.masterclock.R;
import com.kinptm.masterclock.ui.fragment.AlarmClockFragment;
import com.kinptm.masterclock.ui.fragment.LogFragment;
import com.kinptm.masterclock.ui.fragment.SelectAlarmNoticeFragment;
import com.kinptm.masterclock.ui.fragment.SettingFragment;
import com.kinptm.masterclock.ui.fragment.StopWatchFragment;
import com.kinptm.masterclock.ui.fragment.TimerFragment;
import com.kinptm.masterclock.utils.DClickExit;
import com.kinptm.masterclock.utils.ViewUtil;

public class MainActivity extends SherlockFragmentActivity implements ListView.OnItemClickListener {

    private DrawerLayout mDrawerLayout;
    private FrameLayout mContentLayout;
    private ListView mControlerListView;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private DClickExit mExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mExit = new DClickExit(this);

        mDrawerLayout = ViewUtil.findView(this, R.id.main_drawer_layout);
        mContentLayout = ViewUtil.findView(this, R.id.main_content_layout);
        mControlerListView = ViewUtil.findView(this, R.id.main_controler_lv);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.ok, R.string.cancel) {
            @Override
            public void onDrawerOpened(View drawerView) {
                setTitle("功能选择");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                setTitle((String) mControlerListView.getItemAtPosition(mControlerListView.getCheckedItemPosition()));
            }
        };
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
        mControlerListView.setOnItemClickListener(this);
        mControlerListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                getResources().getStringArray(R.array.drawer_controler)));

        getSupportFragmentManager().beginTransaction().replace(mContentLayout.getId(), AlarmClockFragment.newInstance()).commit();
        mControlerListView.setItemChecked(0, true);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mExit.doubleClickExit(keyCode);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (!mDrawerLayout.isDrawerOpen(mControlerListView)) {
                    mDrawerLayout.openDrawer(mControlerListView);
                } else {
                    mDrawerLayout.closeDrawer(mControlerListView);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = AlarmClockFragment.newInstance();
                break;
            case 1:
                fragment = TimerFragment.newInstance();
                break;
            case 2:
                fragment = StopWatchFragment.newInstance();
                break;
            case 3:
                fragment = LogFragment.newInstance();
                break;
            case 4:
                fragment = SettingFragment.newInstance();
                break;
            case 5:
                finish();
//                android.os.Process.killProcess(android.os.Process.myPid());
                return;
        }
        getSupportFragmentManager().beginTransaction().replace(mContentLayout.getId(), fragment).commit();
        mControlerListView.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mControlerListView);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarDrawerToggle.syncState();
    }
}
